﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBatisNet.DataMapper;
using TransCend.DB;
using TransCend.Model;
using TransCend.Util.Page;

namespace TransCend.DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : BaseModel
    {
        protected ICommonDAL GetCommonDAL()
        {
            return CommonDALFactory.GetCommonDAL();
        }

        public T FindByID(int id)
        {
            return GetCommonDAL().FindByID<T>(id);
        }

        /// <summary>
        /// 单个字段条件获取唯一记录对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyname"></param>
        /// <param name="propertyvalue"></param>
        /// <returns></returns>
        public T FindByProperty(string propertyname, object propertyvalue)
        {
            return GetCommonDAL().FindByProperty<T>(propertyname, propertyvalue);
        }

        
        public T FindByPropertys(string[] propertynames, object[] propertyvalues)
        {
            return GetCommonDAL().FindByPropertys<T>(propertynames, propertyvalues);
        }

        public int Insert(T model)
        {
            return GetCommonDAL().Insert(model);
        }

        public int InsertReturnId(T model)
        {
            return GetCommonDAL().InsertReturnId(model);
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Insert(IList<T> models)
        {
            return GetCommonDAL().Insert<T>(models);
        }

        public int Update(T model)
        {
            return GetCommonDAL().Update(model);
        }

        public int Delete(int id)
        {
            return GetCommonDAL().Delete<T>(id);
        }

        public int Delete(int[] ids)
        {
            return GetCommonDAL().Delete<T>(ids);
        }

        /// <summary>
        /// 单个字段删除记录
        /// </summary>
        /// <typeparam name="int"></typeparam>
        /// <param name="propertyname"></param>
        /// <param name="propertyvalue"></param>
        /// <returns></returns>
        public int DeleteByProperty(string propertyname, object propertyvalue)
        {
            return GetCommonDAL().DeleteByProperty<T>(propertyname, propertyvalue);
        }

        /// <summary>
        /// 多个字段删除记录
        /// </summary>
        /// <typeparam name="int"></typeparam>
        /// <param name="propertynames"></param>
        /// <param name="propertyvalues"></param>
        /// <returns></returns>
        public int DeleteByPropertys(string[] propertynames, object[] propertyvalues)
        {
            return GetCommonDAL().DeleteByPropertys<T>(propertynames, propertyvalues);
        }
        /// <summary>
        /// 对主键为string类型的ID删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(string id)
        {
            return GetCommonDAL().Delete<T>(id);
        }

        public int Delete(string DBFied, object value)
        {
            return GetCommonDAL().Delete<T>(DBFied, value);
        }

        /// <summary>
        /// 只更新DBFields中的字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="DBFields"></param>
        /// <returns></returns>
        public int UpdatePartially(T model, string[] DBFields)
        {
            return GetCommonDAL().UpdatePartially<T>(model, DBFields);
        }

        /// <summary>
        /// 对DBField字段计数，默认+1
        /// </summary>
        /// <param name="id"></param>
        /// <param name="DBField"></param>
        /// <returns></returns>
        public int UpdateSetp(int id, string DBField)
        {
            return GetCommonDAL().UpdateSetp<T>(id, DBField);
        }

        /// <summary>
        /// 对DBField字段计数，增+setp
        /// </summary>
        /// <param name="id"></param>
        /// <param name="DBField"></param>
        /// <param name="setp"></param>
        /// <returns></returns>
        public int UpdateSetp(int id, string DBField, int setp)
        {
            return GetCommonDAL().UpdateSetp<T>(id, DBField, setp);
        }

        /// <summary>
        /// 合并，不更新model空值字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Merge(T model)
        {
            return GetCommonDAL().Merge<T>(model);
        }

        /// <summary>
        /// 所有数据
        /// </summary>
        /// <returns></returns>
        public IList<T> FindAll(params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindAll<T>(orderBy);
        }

        /// <summary>
        /// 根据单个条件查询列表数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyname"></param>
        /// <param name="propertyvalue"></param>
        /// <returns></returns>
        public IList<T> FindList(string propertyname, object propertyvalue, params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindList<T>(propertyname, propertyvalue, orderBy);
        }

        /// <summary>
        /// 多个条件查询列表数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertynames"></param>
        /// <param name="propertyvalues"></param>
        /// <returns></returns>
        public IList<T> FindList(string[] propertynames, object[] propertyvalues, params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindList<T>(propertynames, propertyvalues, orderBy);
        }

        public PageTools<T> FindPage(int page, int rows, OrderBy[] orderBy)
        {
            return GetCommonDAL().FindPage<T>(page, rows, orderBy);
        }

        public IList<T> FindList(T queryModel, params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindList<T>(queryModel, orderBy);
        }

        public IList<T> FindList(int[] ids, params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindList<T>(ids, orderBy);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="queryModel"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public PageTools<T> FindPage(int page, int rows, T queryModel, OrderBy[] orderBy)
        {
            return GetCommonDAL().FindPage<T>(page, rows, queryModel, orderBy);
        }

        public Column GetColumn(string tablename, string columnname)
        {
            return GetCommonDAL().GetColumn(tablename, columnname);
        }


        //--------------- IBatis start
        public virtual PageTools<T> IFindPage(string listKey, string countKey, T queryModel)
        {
            ISqlMapper mapper = Mapper.Instance();
            IList<T> list = mapper.QueryForList<T>(listKey, queryModel);
            int count = mapper.QueryForObject<int>(countKey, queryModel);
            PageTools<T> pagetools = new PageTools<T>(queryModel.Page, queryModel.Rows, count, list);
            return pagetools;
        }

        public virtual IList<T> IFindList(string listKey, T queryModel)
        {
            ISqlMapper mapper = Mapper.Instance();
            return mapper.QueryForList<T>(listKey, queryModel);
        }

        public virtual int IUpate(string updateKey, T model)
        {
            ISqlMapper mapper = Mapper.Instance();
            return mapper.Update(updateKey, model);
        }

        public virtual object IInsert(string insertKey, T model)
        {
            ISqlMapper mapper = Mapper.Instance();
            return mapper.Insert(insertKey, model);
        }

        public virtual int IDelete(string deleteKey, object obj)
        {
            ISqlMapper mapper = Mapper.Instance();
            return mapper.Delete(deleteKey, obj);
        }

        public virtual T IFindOne(string selectKey, object obj)
        {
            ISqlMapper mapper = Mapper.Instance();
            return mapper.QueryForObject<T>(selectKey, obj);
        }

        public virtual DateTime FindNowDate()
        {
            return GetCommonDAL().FindNowDate();
        }
        //--------------- IBatis end


        public int FindListCount(T queryModel, params OrderBy[] orderBy)
        {
            return GetCommonDAL().FindListCount<T>(queryModel);
        }
    }
}
